﻿#region MIT Licenze
//The MIT License

//Copyright (c) 2008 Sebastian Faltoni, dotnetfireball.org

//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files (the "Software"), to deal
//in the Software without restriction, including without limitation the rights
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the Software is
//furnished to do so, subject to the following conditions:

//The above copyright notice and this permission notice shall be included in
//all copies or substantial portions of the Software.

//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//THE SOFTWARE.
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Fireball.Extensions;

namespace Fireball.OpenXML.Word
{
    /// <summary>
    /// This is the element for showing an image 
    /// under the document flow as a Run Content
    /// </summary>
    public class Picture : RunContent
    {
        private string _RelationID;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="relId">Relationship id</param>
        internal Picture(string relId)
        {
            _RelationID = relId;
        }

        /// <summary>
        /// Get/Set the picture with in Twentieths of a Point
        /// </summary>
        public int Width
        {
            get;
            set;
        }

        /// <summary>
        /// get/Set the picture Height in Twentieths of a Point
        /// </summary>
        public int Height
        {
            get;
            set;
        }

        public override System.Xml.Linq.XContainer ToXml()
        {
            //XNamespace a = "http://schemas.openxmlformats.org/drawingml/2006/main";
            //XNamespace pic ="http://schemas.openxmlformats.org/drawingml/2006/picture";
            //distT="0" distB="0" distL="0" distR="0"
            var xDrawing = new XElement(NS.WM + "drawing",
                    new XElement(NS.WD + "inline",
                        new XAttribute("distT", 0),
                        new XAttribute("distB", 0),
                        new XAttribute("distL", 0),
                        new XAttribute("distR", 0),

                        new XElement(NS.WD + "extent",
                                new XAttribute("cx", this.Width),
                                new XAttribute("cy", this.Height)
                            ),
                        new XElement(NS.WD + "docPr",
                            new XAttribute("id", "1"),
                            new XAttribute("name", "Immagine 0")),
                    new XElement(NS.DM + "graphic", new XAttribute(XNamespace.Xmlns + "a", NS.DM.NamespaceName),
                    new XElement(NS.DM + "graphicData",
                        new XAttribute("uri", "http://schemas.openxmlformats.org/drawingml/2006/picture"),
                        new XElement(NS.PIC + "pic",
                            new XElement(NS.PIC + "nvPicPr",
                                new XElement(NS.PIC + "cNvPr",
                                    new XAttribute("id", 1),
                                    new XAttribute("name", "Immagine 0")),
                                    new XElement(NS.PIC + "cNvPicPr")
                                    ),
                            new XAttribute(XNamespace.Xmlns + "pic", NS.PIC.NamespaceName),
                            new XElement(NS.PIC + "blipFill",
                                new XElement(NS.DM + "blip",
                                    new XAttribute(NS.RS + "embed", this._RelationID)
                                    ),
                                    new XElement(NS.DM + "stretch",
                                        new XElement(NS.DM + "fillRect"))
                                    ),
                                    new XElement(NS.PIC + "spPr",
                                        new XElement(NS.DM + "xfrm",
                                                new XElement(NS.DM + "off",
                                                    new XAttribute("x", 0),
                                                    new XAttribute("y", 0)),
                                                new XElement(NS.DM + "ext",
                                                    new XAttribute("cx", this.Width),
                                                    new XAttribute("cy", this.Height))
                                        ),
                                        new XElement(NS.DM + "prstGeom",
                                            new XAttribute("prst", "rect"),
                                            new XElement(NS.DM + "avLst"))

                                    ))))));

            return xDrawing;
        }
    }
}
